package com.base.util;

import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JsonUtil {

	public static char[] hex = "0123456789ABCDEF".toCharArray();

	/**
	 * 对json 进行转义处理
	 * <p>
	 * 对特殊字符进行escape, 把非ascii 字符 转换成unicode 编码
	 * 
	 * @param obj
	 * @return
	 */
	public static String escapeJSON(Object obj) {
		StringBuilder sb = new StringBuilder();

		CharacterIterator it = new StringCharacterIterator(obj.toString());

		for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
			if (c == '"') {
				sb.append("\\\"");
			} else if (c == '\\') {
				sb.append("\\\\");
			} else if (c == '/') {
				sb.append("\\/");
			} else if (c == '\b') {
				sb.append("\\b");
			} else if (c == '\f') {
				sb.append("\\f");
			} else if (c == '\n') {
				sb.append("\\n");
			} else if (c == '\r') {
				sb.append("\\r");
			} else if (c == '\t') {
				sb.append("\\t");
			} else if (Character.isISOControl(c)) {
				sb.append(unicode(c));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

	/**
	 * Represent as unicode
	 * 
	 * @param c
	 *            character to be encoded
	 */
	public static String unicode(char c) {
		StringBuilder sb = new StringBuilder();
		sb.append("\\u");

		int n = c;

		for (int i = 0; i < 4; ++i) {
			int digit = (n & 0xf000) >> 12;

			sb.append(hex[digit]);
			n <<= 4;
		}
		return sb.toString();
	}

	/**
	 * 把列表数据转换成json string
	 * 
	 * @param datalist
	 * @return
	 */
	public static String toJSONStr(Object data) {
		return toJSONStr(data, null);
	}

	/**
	 * 把列表数据转换成json string
	 * 
	 * @param datalist
	 * @return
	 */
	public static String toJSONStr(Object data, JsonConfig jsonConfig) {
		JSONArray ja = null;
		if (jsonConfig != null) {
			ja = JSONArray.fromObject(data, jsonConfig);
		} else {
			ja = JSONArray.fromObject(data);
		}
		return ja.toString();
	}

	/**
	 * 将Json串转换成Map
	 * 
	 * @param jsonStr
	 * @return
	 */
	public static Map coverJSONStr(String jsonStr) {
		JSONObject obj = JSONObject.fromObject(jsonStr);
		Object objtemp = JSONObject.toBean(obj, HashMap.class);
		Map map = (Map) objtemp;
		if (map != null) {
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object temp = map.get(key);
				if (temp instanceof MorphDynaBean) {
					Map tempMap = coverJSonBeantoMap(temp);
					map.put(key, tempMap);
				}
			}
		}
		return map;
	}

	private static Map coverJSonBeantoMap(Object bean) {
		JSONObject obj = JSONObject.fromObject(bean);
		Object objtemp = JSONObject.toBean(obj, HashMap.class);
		Map map = (Map) objtemp;
		if (map != null) {
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				String key = it.next();
				Object temp = map.get(key);
				if (temp instanceof MorphDynaBean) {
					Map tempMap = coverJSonBeantoMap(temp);
					map.put(key, tempMap);
				}
			}
		}
		return map;
	}

	public static void main(String[] args) {
		String json = "{ddd:222,aaa:4444,rsel:'1',aa:{d:1,b:2,c:{a:2}}}";
		Map<String, Object> map = coverJSONStr(json);
		Object obj = map.get("aa");
		Map map2 = (Map) obj;
		System.out.println(1);
	}

}
